package com.example.loggingservice.service.impl;

import com.example.loggingservice.dto.LogMessage;
import com.example.loggingservice.entity.OperationLog;
import com.example.loggingservice.mapper.OperationLogMapper;
import com.example.loggingservice.service.LoggingService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class LoggingServiceImpl implements LoggingService {

    private final OperationLogMapper logMapper;

    @Override
    public void saveLog(LogMessage logMessage) {
        try {
            OperationLog operationLog = new OperationLog();
            operationLog.setUserId(logMessage.getUserId());
            operationLog.setAction(logMessage.getAction());
            operationLog.setIp(logMessage.getIp());
            operationLog.setDetail(logMessage.getDetail());
            logMapper.save(operationLog);
            log.info("成功保存日志: {}", operationLog);
        } catch (Exception e) {
            log.error("保存日志失败: {}", logMessage, e);
            // 在实际应用中，您可能希望将此消息发送到死信队列。
        }
    }

    @Override
    public List<OperationLog> getAllLogs() {
        return logMapper.findAll();
    }
} 